package controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.UrlUtility;
import model.PublicHolidayDTO;
import bizlogic.PublicHolidayManager;

/**
 * Servlet implementation class ProcessPublicHolidayServlet
 */
@WebServlet("/ProcessPublicHoliday")
public class ProcessPublicHolidayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	
	//private static final java.util.Date Date = null;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProcessPublicHolidayServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doShow(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}
	private void doShow(HttpServletRequest req, HttpServletResponse res) {

		
		String address = "/WEB-INF/ManagePublicHoliday.jsp";
		String action = req.getParameter("action");
		
		PublicHolidayManager m = null;
		PublicHolidayDTO pbDay = new PublicHolidayDTO();
					
			if (action.trim().equalsIgnoreCase("insert")) {
				m = new PublicHolidayManager();
				req.setAttribute("action", "insert");
				req.setAttribute("publicholidays", pbDay);
				
				UrlUtility.forwardPage(req, res, address);	
				
							
			} 
			else if(action.equalsIgnoreCase("edit")) {
				
				req.setAttribute("action", "edit");
				m = new PublicHolidayManager();
				 String start = req.getParameter("startdate");
				 
				 
				DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
			      java.util.Date datestart = null;
			      
			     try {
			    	
			    	 datestart = df.parse(start);
					
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			    
			    	 java.sql.Date dstart = new java.sql.Date(datestart.getTime());
				      //java.sql.Date dsend = new java.sql.Date(dateend .getTime());
				      
				
				pbDay = m.getholiday(dstart);
				
		    	
				req.setAttribute("publicholidays", pbDay);
				UrlUtility.forwardPage(req, res, address);	
			}
			else
			{
				UrlUtility.redirectPage(res, "LoadPublicHoliday");
			}
			
			


			
			
			
	}
	private void processRequest(HttpServletRequest request, HttpServletResponse response)
	{
		//validate
		//validate
				String address = "/WEB-INF/ManagePublicHoliday.jsp";
				 String start = request.getParameter("startdate");
				 String end = request.getParameter("enddate");
				 String desc = request.getParameter("description");

					 String error = "";
					 boolean valid;
					 PublicHolidayManager m = new PublicHolidayManager();
						PublicHolidayDTO pbDay = new PublicHolidayDTO();
						pbDay.setDescription(request.getParameter("description"));
						String action = (String) request.getParameter("action");
						
						if (action.equalsIgnoreCase("insert")) {
							 
						      DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
						      java.util.Date datestart = null;
						      java.util.Date dateend = null;
						      
						     
						    	  try {
										datestart = df.parse(start);
										dateend = df.parse(end);
									} catch (ParseException e1) {
										// TODO Auto-generated catch block
										e1.printStackTrace();
									}
								 				      
									 try
								     {
								    	 java.sql.Date dstart = new java.sql.Date(datestart.getTime());
									      java.sql.Date dsend = new java.sql.Date(dateend .getTime());
									      
									     // System.out.println(ds);//Mon Jul 05 00:00:00 IST 2010
									      
										 pbDay.setStartdate(dstart);
										 pbDay.setEnddate(dsend);
										 error = m.insertPublicHoliday(pbDay);
										 
								     }
								     catch(NumberFormatException e)
								     {
											valid = false;
											error = "holiday.error.invalidnumbercode";
								     }
							
										
						} 
						else if (action.equalsIgnoreCase("edit")) {
							 
							DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
						      java.util.Date datestart = null;
						      java.util.Date dateend  = null;
						     try {
						    	 datestart = df.parse(start);
								 dateend = df.parse(end);
							} catch (ParseException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
						     try
						     {
						    	 java.sql.Date dstart = new java.sql.Date(datestart.getTime());
							      java.sql.Date dsend = new java.sql.Date(dateend .getTime());
							      
							     // System.out.println(ds);//Mon Jul 05 00:00:00 IST 2010
							      
								 pbDay.setStartdate(dstart);
								 pbDay.setEnddate(dsend);
								 pbDay.setDescription(desc);
								 error =m.updatePublicHoliday(pbDay);
						     }
						     catch(NumberFormatException e)
						     {
									valid = false;
									error = "holiday.error.invalidnumbercode";
						     }
						}
						
							UrlUtility.redirectPage(response, "LoadPublicHoliday");
						

					

		
				
}
}
